package cn.sylinx.redis.springboot;

import java.util.Map;

import redis.clients.jedis.JedisPubSub;

public class RedisPubSub extends JedisPubSub {

	private Map<String, SubscribeMessageHandler<String>> channelsWithHandlers;

	public Map<String, SubscribeMessageHandler<String>> getChannelsWithHandlers() {
		return channelsWithHandlers;
	}

	public void setChannelsWithHandlers(Map<String, SubscribeMessageHandler<String>> channelsWithHandlers) {
		this.channelsWithHandlers = channelsWithHandlers;
	}

	public RedisPubSub() {

	}

	public RedisPubSub(Map<String, SubscribeMessageHandler<String>> channelsWithHandlers) {
		this.channelsWithHandlers = channelsWithHandlers;
	}

	public void onMessage(String channel, String message) {
		channelsWithHandlers.get(channel).handle(message);
	}

	public String[] getChannels() {
		String[] channels = new String[channelsWithHandlers.size()];
		channelsWithHandlers.keySet().toArray(channels);
		return channels;
	}
}
